﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ETOrePriceCalc
{
	/// <summary>
	/// Interaction logic for MainWindow.xaml
	/// </summary>
	public partial class MainWindow : Window
	{
		public List<Mineral> MineralList;
		public ApiParser parser;
        private bool setup;
		public MainWindow()
		{
			InitializeComponent();
			parser = new ApiParser("cyril audeles", 10000002, 0);
			MineralList = setupList();

            #region combobox setup
            setup = true;
            comboBox3.ItemsSource = MineralList;
			comboBox3.DisplayMemberPath = "Name";
            comboBox4.ItemsSource = MineralList;
            comboBox4.DisplayMemberPath = "Name";
            comboBox5.ItemsSource = MineralList;
            comboBox5.DisplayMemberPath = "Name";
            comboBox6.ItemsSource = MineralList;
            comboBox6.DisplayMemberPath = "Name";
            comboBox7.ItemsSource = MineralList;
            comboBox7.DisplayMemberPath = "Name";
            comboBox8.ItemsSource = MineralList;
            comboBox8.DisplayMemberPath = "Name";
            comboBox9.ItemsSource = MineralList;
            comboBox9.DisplayMemberPath = "Name";
            comboBox10.ItemsSource = MineralList;
            comboBox10.DisplayMemberPath = "Name";
            comboBox11.ItemsSource = MineralList;
            comboBox11.DisplayMemberPath = "Name";
            comboBox12.ItemsSource = MineralList;
            comboBox12.DisplayMemberPath = "Name";
            comboBox3.SelectedIndex = 0;
            comboBox4.SelectedIndex = 0;
            comboBox5.SelectedIndex = 0;
            comboBox6.SelectedIndex = 0;
            comboBox7.SelectedIndex = 0;
            comboBox8.SelectedIndex = 0;
            comboBox9.SelectedIndex = 0;
            comboBox10.SelectedIndex = 0;
            comboBox11.SelectedIndex = 0;
            setup = false;
            comboBox12.SelectedIndex = 0;
            #endregion
        }

		/// <summary>
		/// fills the mineral list with all the required minerals
		/// </summary>
		/// <returns>returns the filled list</returns>
		private List<Mineral> setupList()
		{
			List<Mineral> retval = new List<Mineral>();

            //all minerals to add
            #region minerals adding
            retval.Add(new Mineral("Veldspar"));
			retval.Add(new Mineral("Concentrated Veldspar"));
			retval.Add(new Mineral("Dense Veldspar"));
			retval.Add(new Mineral("Scordite"));
			retval.Add(new Mineral("Condensed Scordite"));
			retval.Add(new Mineral("Massive Scordite"));
			retval.Add(new Mineral("Pyroxeres"));
			retval.Add(new Mineral("Solid Pyroxeres"));
			retval.Add(new Mineral("Viscous Pyroxeres"));
			retval.Add(new Mineral("Plagioclase"));
			retval.Add(new Mineral("Azure Plagioclase"));
			retval.Add(new Mineral("Rich Plagioclase"));
			retval.Add(new Mineral("Omber"));
			retval.Add(new Mineral("Silvery Omber"));
			retval.Add(new Mineral("Golden Omber"));
			retval.Add(new Mineral("Kernite"));
			retval.Add(new Mineral("Luminous Kernite"));
			retval.Add(new Mineral("Fiery Kernite"));
			retval.Add(new Mineral("Jaspet"));
			retval.Add(new Mineral("Pure Jaspet"));
			retval.Add(new Mineral("Pristine Jaspet"));
			retval.Add(new Mineral("Hemorphite"));
			retval.Add(new Mineral("Vivid Hemorphite"));
			retval.Add(new Mineral("Radiant Hemorphite" ));
			retval.Add(new Mineral("Hedbergite"));
			retval.Add(new Mineral("Vitric Hedbergite"));
			retval.Add(new Mineral("Glazed Hedbergite" ));
			retval.Add(new Mineral("Gneiss"));
			retval.Add(new Mineral("Iridescent Gneiss")); 
			retval.Add(new Mineral("Prismatic Gneiss"));
			retval.Add(new Mineral("Dark Ochre"));
			retval.Add(new Mineral("Onyx Ochre"));
			retval.Add(new Mineral("Obsidian Ochre"));
			retval.Add(new Mineral("Crokite"));
			retval.Add(new Mineral("Sharp Crokite"));
			retval.Add(new Mineral("Crystalline Crokite"));
			retval.Add(new Mineral("Spodumain"));
			retval.Add(new Mineral("Bright Spodumain"));
			retval.Add(new Mineral("Gleaming Spodumain"));
			retval.Add(new Mineral("Bistot"));
			retval.Add(new Mineral("Triclinic Bistot"));
			retval.Add(new Mineral("Monoclinic Bistot"));
			retval.Add(new Mineral("Arkonor"));
			retval.Add(new Mineral("Crimson Arkonor"));
			retval.Add(new Mineral("Prime Arkonor"));
			retval.Add(new Mineral("Mercoxit"));
			retval.Add(new Mineral("Magma Mercoxit"));
			retval.Add(new Mineral("Vitreous Mercoxit"));
            #endregion


            return retval;
		}

		/// <summary>
		/// changes the stock of mineral to amount
		/// </summary>
		/// <param name="mineral">name of the mineral</param>
		/// <param name="amount">new stock of mineral</param>
		public void setStock(string mineral,int amount)
		{
			//find the mineral class
			Mineral target = null;
			foreach (Mineral m in MineralList)
			{
				if (m.Name == mineral)
				{
					target = m;
					break;
				}
			}

			//set the stock of the mineral to the amount
			if (target != null)
				target.Stock = amount;
		}

		/// <summary>
		/// gets the current stock of the mineral
		/// </summary>
		/// <param name="mineral">name of the mineral to lookup</param>
		/// <returns>return the current stock of mineral</returns>
		public int getStock(string mineral)
		{
			foreach (Mineral m in MineralList)
			{
				if (m.Name == mineral)
					return m.Stock;
			}
			return 0;
		}

		/// <summary>
		/// updates all checkboxes to their suposed value
		/// </summary>
		public void updateTextboxes()
		{
            if (setup) return;
			textBox1.Text = getStock(((Mineral)comboBox3.SelectedItem).Name).ToString();
			textBox2.Text = getStock(((Mineral)comboBox4.SelectedItem).Name).ToString();
			textBox3.Text = getStock(((Mineral)comboBox5.SelectedItem).Name).ToString();
			textBox4.Text = getStock(((Mineral)comboBox6.SelectedItem).Name).ToString();
			textBox5.Text = getStock(((Mineral)comboBox7.SelectedItem).Name).ToString();
			textBox6.Text = getStock(((Mineral)comboBox8.SelectedItem).Name).ToString();
			textBox7.Text = getStock(((Mineral)comboBox9.SelectedItem).Name).ToString();
			textBox8.Text = getStock(((Mineral)comboBox10.SelectedItem).Name).ToString();
			textBox9.Text = getStock(((Mineral)comboBox11.SelectedItem).Name).ToString();
			textBox10.Text = getStock(((Mineral)comboBox12.SelectedItem).Name).ToString();
		}


		//events to update the stock when a new stock is filled in
#region  textboxes commit changes
		//textbox 1 is matches to combobox3
		private void textBox1_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox1.Text);

				setStock(comboBox3.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox2_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox2.Text);

				setStock(comboBox4.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox3_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox3.Text);

				setStock(comboBox5.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox4_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox4.Text);

				setStock(comboBox6.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox5_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox5.Text);

				setStock(comboBox7.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox6_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox6.Text);

				setStock(comboBox8.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox7_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox7.Text);

				setStock(comboBox9.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox8_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox8.Text);

				setStock(comboBox10.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox9_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox9.Text);

				setStock(comboBox11.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}

		private void textBox10_LostFocus(object sender, RoutedEventArgs e)
		{
			int newStock = 0;
			try
			{
				newStock = Convert.ToInt32(textBox10.Text);

				setStock(comboBox12.Text, newStock);
				updateTextboxes();
			}
			catch (FormatException ex)
			{
				MessageBox.Show("please enter a number");
			}
		}
#endregion 

        //events to update the shown stock when a new mineral is selected
		private void comboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
		{
			updateTextboxes();
		}

		private void btRefresh_Click(object sender, RoutedEventArgs e)
		{
			parser.GetPrices();
            parser.SetPrices(MineralList);
		}

        private void btTotal_Click(object sender, RoutedEventArgs e)
        {
            double total = 0;

            foreach (Mineral m in MineralList)
            {
                total += m.getValue();
            }

            tbTotal.Text = total.ToString();
        }		
	}
}
